package com.xps.and.yuntong.View.Custom;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;
import android.support.v4.widget.ViewDragHelper;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;

import com.xps.and.yuntong.R;
import com.jude.utils.JUtils;

public class SliderControlView extends FrameLayout {
    int mWidth;
    int mHeight;
    int parentGapWidth = JUtils.dip2px(2);
    int cornerPixel = JUtils.dip2px(5);
    SwitchListener mSwitchListener;
    ViewDragHelper mDragger;
    View vSliderBlock;
    Point scp = new Point();
    RectF filledAreaRect = new RectF();
    Paint filledAreaPaint = new Paint();
    boolean completed;

    public SliderControlView(Context context) {
        super(context);
    }

    public SliderControlView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mDragger = ViewDragHelper.create(this, 1.0f, new ViewDragHelper.Callback() {
            @Override
            public boolean tryCaptureView(View child, int pointerId) {
                return child.getId() == R.id.v_slider_block;
            }

            @Override
            public int clampViewPositionHorizontal(View child, int left, int dx) {
                final int leftBound = getPaddingLeft();
                final int rightBound = getWidth() - vSliderBlock.getWidth() - leftBound;
                return Math.min(Math.max(left, leftBound), rightBound);
            }

            @Override
            public int clampViewPositionVertical(View child, int top, int dy) {
                final int topBound = getPaddingTop();
                final int bottomBound = getHeight() - vSliderBlock.getHeight() - topBound;
                return Math.min(Math.max(top, topBound), bottomBound);
            }

            @Override
            public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {
                super.onViewPositionChanged(changedView, left, top, dx, dy);
                invalidate();
                if (left > mWidth - (vSliderBlock.getWidth() * 2)) {
                    if(mSwitchListener!=null&&!completed){
                        mSwitchListener.onCompleted();
                    }
//                    completed = true;
                }
            }

            @Override
            public void onViewReleased(View releasedChild, float xvel, float yvel) {
                super.onViewReleased(releasedChild, xvel, yvel);
                if (releasedChild == vSliderBlock) {
                    mDragger.settleCapturedViewAt(scp.x, scp.y);
                    invalidate();
                }
            }
        });

        setWillNotDraw(false);
        filledAreaPaint.setColor(Color.parseColor("#2ebf12"));
        filledAreaPaint.setAntiAlias(true);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        filledAreaRect.set(parentGapWidth-2, parentGapWidth, vSliderBlock.getX() + vSliderBlock.getWidth() - parentGapWidth, mHeight - parentGapWidth);
        canvas.drawRoundRect(filledAreaRect, cornerPixel, cornerPixel, filledAreaPaint);
    }

    @Override
    public void computeScroll() {
        if (mDragger.continueSettling(true)) {
            invalidate();
        }
    }

    public SliderControlView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setSwitchListener(SwitchListener l) {
        this.mSwitchListener = l;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        vSliderBlock = getChildAt(0);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = w;
        mHeight = h;
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        mWidth = getMeasuredWidth();
        scp.x = vSliderBlock.getLeft();
        scp.y = vSliderBlock.getTop();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        mDragger.processTouchEvent(event);
        return true;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return mDragger.shouldInterceptTouchEvent(ev);
    }

    public interface SwitchListener {
        void onCompleted();
    }

}
